package grsu.epam.airline.action;

import grsu.epam.airline.form.CrewForm;
import grsu.epam.airline.service.FlightCrewService;
import grsu.epam.airline.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class FlightCrewAddEmployeeAction extends Action {
	String target = ForwardActionTo.UPDATEFLIGHTCREW;

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
		UserService userService = new UserService();
		if (userService.checkUser(session.getAttribute("auth"),
				ConstantUserRole.DISPATCHER)) {
			CrewForm input = (CrewForm) form;
			FlightCrewService flightCrewService = new FlightCrewService();
			flightCrewService.updateFlightCrew(input.getFlightCrewId(),
					input.getEmployeeId());
			request.setAttribute("flightCrew", flightCrewService
					.getFlightCrewById(input.getFlightCrewId()));
		} else {
			target = ForwardActionTo.ERRORUSER;
		}
		return mapping.findForward(target);
	}
}
